package com.cangxuan.backend.scheduled;

import com.alibaba.fastjson.JSONObject;
import com.cangxuan.backend.bean.Result;
import com.cangxuan.backend.dao.IOrderFormDao;
import com.cangxuan.backend.dao.IUserDao;
import com.cangxuan.backend.entity.OrderForm;
import com.cangxuan.backend.entity.User;
import com.cangxuan.backend.service.IOrderFormService;
import com.cangxuan.backend.service.IRegistrationService;
import com.cangxuan.backend.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@EnableScheduling
public class CancelOvertimeOrderFromScheduled {

    IOrderFormDao orderFormDao;
    DataSourceTransactionManager transactionManager;

    IRegistrationService registrationService;

    IOrderFormService orderFormService;
    RestTemplate restTemplate;

    IUserDao userDao;

    @Autowired
    public void setUserDao(IUserDao userDao) {
        this.userDao = userDao;
    }

    @Autowired
    public void setRestTemplate(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    @Autowired
    public void setOrderFormService(IOrderFormService orderFormService) {
        this.orderFormService = orderFormService;
    }

    @Autowired
    public void setRegistrationService(IRegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    @Autowired
    public void setOrderFormDao(IOrderFormDao orderFormDao) {
        this.orderFormDao = orderFormDao;
    }

    @Autowired
    public void setTransactionManager(DataSourceTransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Scheduled(fixedDelay = 1000 * 60 * 1)
    public void scheduledTask() {
        List<OrderForm> list = orderFormDao.selectOverTimeOderForm();
        if (list.size() != 0) {
            for (OrderForm orderForm : list) {
                if (orderForm.getRegistrationId() != 0) {
                    List<OrderForm> orderFormList=new ArrayList<>();
                    orderFormList.add(orderForm);
                    int res = registrationService.cancelPay(orderFormList);
                    if (res==2){
                        sendNotification(orderForm);
                    }
                } else {
                    int res = orderFormService.cancelOderForm(orderForm.getId());
                    if (res==1){
                        sendNotification(orderForm);
                    }
                }
            }
        }

    }

    private void sendNotification(OrderForm orderForm) {
        int buyerId = orderForm.getBuyerId();
        User user = userDao.selectById(buyerId);
        new Thread(()->{
            JSONObject body = new JSONObject();
            body.put("registrationId", user.getRegistrationId());
            body.put("message", "你的编号为："+ orderForm.getId()+"的订单，因超时而自动取消，请到详情页面进行查看");
            Result result = restTemplate.postForObject("http://CangXuanPushService/api/pushMessageToUser", body, Result.class);
            if (result.getMessage().equals("OK")) {
                log.info("发送通知成功","orderformId:"+ orderForm.getId());
            } else {
                log.info("发送通知失败","orderformId:"+ orderForm.getId());
            }
        }).start();
    }

}
